<script setup>
import {
    HomeFilled,         // 主页管理
    Document,           // 新闻动态管理
    UserFilled,         // 成员信息管理
    Trophy,             // 实验室荣誉管理
    Collection,         // 学术论文管理
    Briefcase,          // 科研项目管理
    Flag,               // 党建管理
    Setting,            // 设置关于我们
    SwitchButton,       // 登出按钮
    CaretBottom         // 下拉菜单箭头
} from '@element-plus/icons-vue'
import ip from '@/utils/baseURL.js';

import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { ref, onMounted, onUpdated } from "vue";

const router = useRouter();
// 处理下拉菜单项的点击事件
const handleCommand = async (command) => {
  if (command === 'logout') {
    try {
      // 获取当前用户的 session
      const session = localStorage.getItem('session');

      // 调用后端登出接口
      const response = await fetch(ip + '/inter/login/logout', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Session': `${session}`, // 传递 session
        },
      });
      const data = await response.json();

      // 处理返回结果
      if (data.code === 200) {
        ElMessage.success('登出成功');
        // 清除前端的用户登录状态
        localStorage.removeItem('session');
        // 跳转到登录页面
        router.push('/user/index/main');
      } else {
        ElMessage.error(data.message || '登出失败');
      }
    } catch (error) {
      console.error('Error:', error);
      ElMessage.error('网络错误，请稍后重试');
    }
  }
};


// 获取当前时间
const currentTime = ref('');

// 方法：更新当前时间
const updateTime = () => {
  const now = new Date();
  const formattedTime = now.toLocaleString(); // 格式化为本地时间字符串
  currentTime.value = formattedTime;
};

// 挂载时更新时间，并每秒更新
onMounted(() => {
  updateTime();
  setInterval(updateTime, 1000);
});



</script>

<template>
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
            <el-menu active-text-color="#ffd04b" background-color="#86B943"  text-color="#fff"
                router>
                <el-menu-item index="/admin/index/main">
                    <el-icon>
                        <HomeFilled />
                    </el-icon>
                    <span>主页管理</span>
                </el-menu-item>
                <el-menu-item index="/admin/newsPage">
                    <el-icon>
                        <Document />
                    </el-icon>
                    <span>党建新闻动态管理</span>
                </el-menu-item>
                <el-menu-item index="/admin/memberPage">
                    <el-icon>
                        <UserFilled />
                    </el-icon>
                    <span>成员信息管理</span>
                </el-menu-item>
                <el-menu-item index="/admin/honorPage">
                    <el-icon>
                        <Trophy />
                    </el-icon>
                    <span>实验室荣誉管理</span>
                </el-menu-item>
                <el-menu-item index="/admin/paperPage">
                    <el-icon>
                        <Collection />
                    </el-icon>
                    <span>学术论文管理</span>
                </el-menu-item>
                <el-menu-item index="/admin/projectPage">
                    <el-icon>
                        <Briefcase />
                    </el-icon>
                    <span>科研项目管理</span>
                </el-menu-item>
                <!-- <el-menu-item index="/admin/partyPage">
                    <el-icon>
                        <Flag />
                    </el-icon>
                    <span>党建管理</span>
                </el-menu-item> -->
                <el-menu-item index="/admin/aboutPage">
                    <el-icon>
                        <Setting />
                    </el-icon>
                    <span>设置关于我们</span>
                </el-menu-item>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <strong>欢迎您管理员</strong>
                <span class="time"><strong>{{ currentTime }}</strong></span>
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="logout" :icon="SwitchButton">登出</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main class="fixed-width-main">
                <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>智慧农业创新实验室 ©2025 Created by HYH</el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  min-width: 1400px; /* 设置整体最小宽度 */

  .el-aside {
    background-color: #86B943;
    width: 200px !important; /* 固定左侧菜单宽度 */
    flex: 0 0 200px; /* 禁止伸缩 */

    &__logo {
      height: 120px;
      //background: url('@/assets/logo.png') no-repeat center / 120px auto;
    }

    .el-menu {
      border-right: none;
      flex-shrink: 0; /* 禁止菜单收缩 */
    }
  }

  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 60px;
    flex-shrink: 0; /* 禁止头部收缩 */
    min-width: 1400px; /* 头部最小宽度 */

    .el-dropdown__box {
      display: flex;
      align-items: center;

      .el-icon {
        color: #999;
        margin-left: 10px;
      }
    }

    .time {
      flex-shrink: 0; /* 禁止时间模块收缩 */
      margin: 0 20px;
    }
  }

  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
    height: 50px;
    flex-shrink: 0; /* 禁止底部收缩 */
    min-width: 1400px;
  }
}

.fixed-width-main {
  flex: 1;
  min-width: 1200px; /* 主内容区最小宽度 */
  overflow-x: auto; /* 横向滚动 */
  padding: 20px;
  background: #f5f7fa;
  
  /* 滚动条样式 */
  &::-webkit-scrollbar {
    height: 8px;
    background: #f1f1f1;
  }
  &::-webkit-scrollbar-thumb {
    background: #c0c4cc;
    border-radius: 4px;
  }
}

/* 保证子元素固定尺寸 */
.el-menu-item {
  min-width: 200px !important;
  flex-shrink: 0;
}

/* 头部布局优化 */
.el-header {
  > * {
    flex-shrink: 0;
  }
  
  strong {
    white-space: nowrap; /* 防止文字换行 */
  }
  
  .time {
    flex: 1;
    text-align: right;
    padding-right: 20px;
  }
}
</style>